﻿<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/AdminDefault.Master"
  AutoEventWireup="true" CodeBehind="Advertisement.aspx.cs" Inherits="ITViet.Web.Admin.Advertisement" %>

<%@ Register Assembly="DevExpress.Web.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxCallback" TagPrefix="dxcb" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dxe" %>
<%@ Register Assembly="DevExpress.Web.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxPopupControl" TagPrefix="dxpc" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dxwgv" %>
<%@ Register Assembly="DevExpress.Web.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxUploadControl" TagPrefix="dxuc" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  <div style="clear: both; padding-top: 10px;">
    <dxwgv:ASPxGridView ID="gvAd" runat="server" AutoGenerateColumns="False" ClientInstanceName="gvAd"
      OnCustomCallback="gvAd_CustomCallback" OnCustomJSProperties="gvAd_CustomJSProperties"
      Width="100%" OnHtmlDataCellPrepared="gvAd_HtmlDataCellPrepared" KeyFieldName="Id">
      <SettingsPager>
        <AllButton>
          <Image Width="28px" />
        </AllButton>
        <FirstPageButton>
          <Image Width="23px" />
        </FirstPageButton>
        <LastPageButton>
          <Image Width="23px" />
        </LastPageButton>
        <NextPageButton>
          <Image Width="19px" />
        </NextPageButton>
        <PrevPageButton>
          <Image Width="19px" />
        </PrevPageButton>
        <Summary AllPagesText="<%$Resources:Strings, PagerText %>" Text="<%$Resources:Strings, PagerText %>" />
      </SettingsPager>
      <Images>
        <CollapsedButton Width="13px" />
        <ExpandedButton Width="13px" />
        <DetailCollapsedButton Width="13px" />
        <DetailExpandedButton Width="13px" />
        <HeaderFilter
          Width="19px" />
        <HeaderActiveFilter
          Width="19px" />
        <HeaderSortDown Width="6px" />
        <HeaderSortUp Width="6px" />
        <FilterRowButton Width="13px" />
        <CustomizationWindowClose Width="18px" />
        <PopupEditFormWindowClose Width="18px" />
        <WindowResizer Width="12px" />
        <FilterBuilderClose Width="18px" />
      </Images>
      <Columns>
        <dxwgv:GridViewDataTextColumn VisibleIndex="0" Width="40px">
          <DataItemTemplate>
            <img alt="" src="../images/delete.png" onclick="onDelete('<%#Eval("Id")%>')" style="cursor: pointer;" />
            <img alt="" src="../images/edit.png" onclick="onEdit('<%#Eval("Id")%>','<%#ParseHtml(Eval("Name"))%>','<%#Eval("Website")%>','<%#Eval("ImagePath")%>','<%#Eval("Position")%>')"
              style="cursor: pointer;" />
          </DataItemTemplate>
        </dxwgv:GridViewDataTextColumn>
        <dxwgv:GridViewDataImageColumn FieldName="ImagePath" VisibleIndex="1" Caption="<%$Resources:Strings, Image %>">
          <HeaderStyle HorizontalAlign="Center" />
          <PropertiesImage ImageWidth="100px" ImageHeight="40px">
          </PropertiesImage>
        </dxwgv:GridViewDataImageColumn>
        <dxwgv:GridViewDataTextColumn FieldName="Name" VisibleIndex="2" Caption="<%$Resources:Strings, Name %>">
          <HeaderStyle HorizontalAlign="Center" />
        </dxwgv:GridViewDataTextColumn>
        <dxwgv:GridViewDataHyperLinkColumn FieldName="Website" VisibleIndex="3" Caption="<%$Resources:Strings, Website %>">
          <PropertiesHyperLinkEdit NavigateUrlFormatString="javascript:window.open('{0}');void 0;">
          </PropertiesHyperLinkEdit>
          <HeaderStyle HorizontalAlign="Center" />
        </dxwgv:GridViewDataHyperLinkColumn>
        <dxwgv:GridViewDataTextColumn FieldName="Position" VisibleIndex="5" Visible="False">
          <HeaderStyle HorizontalAlign="Center" />
        </dxwgv:GridViewDataTextColumn>
        <dxwgv:GridViewDataTextColumn Caption="<%$ Resources:Strings, ShowLeft %>" FieldName="IsShow"
          VisibleIndex="4" Width="20px" Visible="False">
          <DataItemTemplate>
            <input type="checkbox" id="ckbShowLeft" checked='<%#Eval("IsShow").ToString() == "True"?true:false %>'
              style="cursor: pointer;" runat="server" name="ckbLeft" />
          </DataItemTemplate>
          <HeaderStyle HorizontalAlign="Center" />
          <CellStyle HorizontalAlign="Center">
          </CellStyle>
        </dxwgv:GridViewDataTextColumn>
      </Columns>
      <ImagesEditors>
        <CalendarPrevYear Width="23px" />
        <CalendarPrevMonth Width="19px" />
        <CalendarNextMonth Width="19px" />
        <CalendarNextYear Width="23px" />
      </ImagesEditors>
      <ClientSideEvents EndCallback="function(s, e){ongvAd_EndCallback(s, e);}" />
    </dxwgv:ASPxGridView>
  </div>
  <div style="padding-top: 10px;">
    <dxe:ASPxButton ID="btnAdd" runat="server" Text="<%$ Resources:Strings, Add %>" AutoPostBack="false">
      <ClientSideEvents Click="function(s,e){onAdd();}" />
    </dxe:ASPxButton>
  </div>
  <div class="CommonFont">
    <dxpc:ASPxPopupControl ID="pcInfo" ClientInstanceName="pcInfo" runat="server" Width="600px"
      HeaderText="<%$ Resources:Strings, Info %>"
      PopupHorizontalAlign="WindowCenter" PopupVerticalAlign="WindowCenter" 
      Modal="True">
      <SizeGripImage Width="12px" />
      <ContentCollection>
        <dxpc:PopupControlContentControl>
          <div style="padding-left: 5px; padding-bottom: 6px;" class="commonfont">
            <dxe:ASPxLabel ID="lblErrorMessage" ClientInstanceName="lblErrorMessage" runat="server"
              Font-Bold="true" ForeColor="Red">
            </dxe:ASPxLabel>
          </div>
          <table style="padding: 5px 0 0 10px;" class="CommonFont">
            <tr>
              <td style="padding-top: 3px; padding-top: 30px;" width="100px">
                <asp:Label ID="Label2" runat="server" Text="<%$Resources:Strings, Image %>"></asp:Label>
                :
              </td>
              <td style="padding-top: 3px;">
                <div style="border: 1px dashed #808080; float: left; width: 110px; height: 110px;
                  vertical-align: middle;" id="divObject">
                </div>
                <div style="float: left; padding-left: 10px; padding-top: 50px;">
                  <dxuc:ASPxUploadControl ID="uploader" runat="server" 
                    ClientInstanceName="uploader" Size="25" OnFileUploadComplete="uploader_FileUploadComplete"
                    FileUploadMode="OnPageLoad">
                    <ClientSideEvents FileUploadComplete="function(s, e) { Uploader_OnUploadComplete(e); }"
                      FileUploadStart="function(s, e) { Uploader_OnUploadStart(); }" TextChanged="function(s,e){uploadFile();}">
                    </ClientSideEvents>
                    <ValidationSettings AllowedContentTypes="image/jpeg,image/gif,image/png,image/pjpeg,application/x-shockwave-flash"
                      MaxFileSize="10485760" FileDoesNotExistErrorText="NOT_EXIST" GeneralErrorText="GENERAL_ERR"
                      MaxFileSizeErrorText="MAX_SIZE" NotAllowedContentTypeErrorText="ERROR_TYPE">
                      <ErrorStyle Font-Size="0pt" />
                    </ValidationSettings>
                  </dxuc:ASPxUploadControl>
                  <asp:Literal ID="Literal1" runat="server" Text="<%$Resources:Messages, ERR0024 %>"></asp:Literal>
                </div>
                <asp:HiddenField ID="hfImagePath" runat="server" />
              </td>
            </tr>
            <tr>
              <td style="padding-top: 3px;">
                <asp:Label ID="Label1" runat="server" Text="<%$Resources:Strings, Name %>"></asp:Label>
                :
              </td>
              <td style="padding-top: 3px;">
                <dxe:ASPxTextBox ID="txtName" ClientInstanceName="txtName" runat="server" 
                  Width="350px">
                </dxe:ASPxTextBox>
              </td>
            </tr>
            <tr>
              <td style="padding-top: 3px;">
                <asp:Label ID="Label3" runat="server" Text="<%$ Resources:Strings, Website %>"></asp:Label>
                :
              </td>
              <td style="padding-top: 3px;">
                <dxe:ASPxTextBox ID="txtWebsite" runat="server" ClientInstanceName="txtWebsite" 
                  Width="350px">
                </dxe:ASPxTextBox>
              </td>
            </tr>
            <tr style="display: none;">
              <td style="padding-top: 3px;">
                <asp:Label ID="Label5" runat="server" Text="<%$ Resources:Strings, Position %>"></asp:Label>
                :
              </td>
              <td>
                <dxe:ASPxComboBox ID="cbxPosition" runat="server" 
                  ClientInstanceName="cbxPosition" DataSourceID="xdsPosition" TextField="Value" 
                  ValueField="Id" ValueType="System.String">
                  <ButtonStyle Width="13px">
                  </ButtonStyle>
                  <ValidationSettings>
                    <ErrorImage Width="14px" />
                  </ValidationSettings>
                </dxe:ASPxComboBox>
              </td>
            </tr>
            <tr>
              <td>
              </td>
              <td style="padding-top: 5px;">
                <div style="float: left">
                  <dxe:ASPxButton ID="btnSave" runat="server" 
                    Text="<%$ Resources:Strings, Save %>">
                    <ClientSideEvents Click="function(s, e){onSave();e.processOnServer = false;}" />
                  </dxe:ASPxButton>
                </div>
                <div style="float: left; padding-left: 3px;">
                  <dxe:ASPxButton ID="btnCancel" runat="server" 
                    Text="<%$ Resources:Strings, Cancel %>">
                    <ClientSideEvents Click="function(s, e){onCancel();e.processOnServer = false;}" />
                  </dxe:ASPxButton>
                </div>
              </td>
            </tr>
          </table>
        </dxpc:PopupControlContentControl>
      </ContentCollection>
      <CloseButtonImage Width="17px" />
    </dxpc:ASPxPopupControl>
  </div>
  <dxcb:ASPxCallback ID="ctrlCallback" ClientInstanceName="ctrlCallback" runat="server"
    OnCallback="ctrlCallback_Callback">
    <ClientSideEvents CallbackComplete="function(s,e){onctrlCallback_CallbackComplete(s,e);}" />
  </dxcb:ASPxCallback>
  <asp:HiddenField ID="hfAdvertiseId" runat="server" />
  <asp:XmlDataSource ID="xdsPosition" runat="server" DataFile="~/App_Data/Status.xml"
    XPath="Items/AdvertisementPosition/Position"></asp:XmlDataSource>

  <script type="text/javascript" language="javascript">
    function onSave() {
      if (checkInput()) {
        gvAd.PerformCallback('Save|');
      }
    }

    function onEdit(id, name, website, imagePath, position) {
      resetValue();
      document.getElementById('<%=hfAdvertiseId.ClientID %>').value = id;
      txtName.SetText(name);
      txtWebsite.SetText(website);
      cbxPosition.SetValue(position);
      if (imagePath != '') {
        bindObject(imagePath, '');
      }
      pcInfo.Show();
    }

    function onAdd() {
      resetValue();
      pcInfo.Show();
    }

    function resetValue() {
      txtName.SetText('');
      txtWebsite.SetText('');
      var divObject = document.getElementById('divObject');
      divObject.innerHTML = '';
      document.getElementById('<%=hfAdvertiseId.ClientID %>').value = '';
      lblErrorMessage.SetText('');
      if (cbxPosition.GetItemCount() > 0)
        cbxPosition.SetSelectedIndex(0);
    }

    function onDelete(id) {
      if (confirm('<%=GetGlobalResourceObject("Messages", "ERR0006") %>'))
        gvAd.PerformCallback('Delete|' + id);
    }

    function onCancel() {
      pcInfo.Hide();
    }

    function checkInput() {
      var error = '';
      if (txtName.GetText() == '')
        error += '<BR> + <%=GetGlobalResourceObject("Strings", "Name") %>';
      if (error != '') {
        error = '<%=GetGlobalResourceObject("Messages", "ERR0008") %>' + error;
        lblErrorMessage.SetText(error);
        return false;
      }
      return true;
    }

    function ongvAd_EndCallback(s, e) {
      var error = gvAd.cpInnerError;
      if (error != '') {
        if (error.indexOf('Save') >= 0) {
          var para = error.split('|');
          if (para[1] != '') {
            lblErrorMessage.SetText(para[1]);
            return false;
          }
          else {
            pcInfo.Hide();
            alertSuccess();
          }
        }
      }
    }

    function Uploader_OnUploadComplete(args) {
      var imgSrc = "../images/ImagePreview.png";
      var itemObject = '';
      var files = args.callbackData.split('|');
      if (args.isValid) {
        imgSrc = "../Upload/Temp/" + files[0];
      }
      else {
        if (args.errorText != '') {
          var error = args.errorText;
          switch (args.errorText) {
            case 'NOT_EXIST':
              error = '<%=GetGlobalResourceObject("Messages", "ERR0020") %>';
              break;
            case 'GENERAL_ERR':
              error = '<%=GetGlobalResourceObject("Messages", "ERR0022") %>';
              break;
            case 'MAX_SIZE':
              error = '<%=GetGlobalResourceObject("Messages", "ERR0023") %>';
              break;
            case 'ERROR_TYPE':
              error = '<%=GetGlobalResourceObject("Messages", "ERR0021") %>';
              break;
          }
          alertMessage(error);
          return;
        }
        if (args.callbackData.indexOf('ERROR') >= 0) {
          var error = args.callbackData.split('|');
          alertMessage(error[1]);
        }
        else
          return;
      }
      bindObject(imgSrc, '../Upload/Temp');
      document.getElementById('<%=hfImagePath.ClientID %>').value = 'Upload/Images/' + files[1];
    }

    function bindObject(file, folder) {
      if (file == '')
        return;
      var type = file.substring(file.lastIndexOf('.') + 1);
      var item = '';
      var divObject = document.getElementById('divObject');
      if (type == 'swf') {
        item = '<embed height=110px width=110px flashvars=\'file=' + file + '\'';
        item += 'wmode=\'transparent\' allowscriptaccess=\'always\' allowfullscreen=\'true\' quality=\'high\'';
        item += 'name=\'player_flash\' id=\'player_flash\' style=\'\' src=\'' + file + '\'';
        item += 'type=\'application/x-shockwave-flash\'>';
      }
      else {
        item = '<img style=\'width:110px;height:110px;\' alt=\'\' src=\'' + file + '\'> </img>';
      }
      divObject.innerHTML = item;
    }

    function Uploader_OnUploadStart() {
    }

    function uploadFile() {
      uploader.Upload();
    }

    function onShowLeftChecked(adId, checked) {
      gvAd.PerformCallback('ShowLeft|' + adId + '|' + checked);
    }

    function EnableEdit(enabled) {
      speLeftQtt.SetEnabled(enabled);
      btnSaveQtt.SetEnabled(enabled);
      btnEditQtt.SetEnabled(!enabled);
    }

    function onctrlCallback_CallbackComplete(s, e) {
      if (e.result != '') {
        alertMessage(e.result);
        return;
      }
      EnableEdit(false);
    }                  
  </script>

</asp:Content>
